package com.tem.product.controller;

import com.api.manage.pojo.product.SysCategoryBean;
import com.api.manage.redis.RedisCacheUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.product.entity.Category;
import com.api.manage.pojo.product.CategoryBean;
import com.tem.product.service.ICategoryService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 系统 前端控制器
 * </p>
 * @author 陈辉
 * @since 2023-11-08
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController extends BaseController{
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    @ApiOperation(value = "系统列表")
    @RequestMapping("list")
    public ResultUtil<IPage<CategoryBean>> list(CategoryBean request, HttpServletRequest req){
        if(request.getSysCode()!=null){//子系统
            Long systemLevelId=redisCacheUtil.getSystemLevelId(this.getSysBranchOfficeId(req),request.getSysCode());
            if(systemLevelId==null)
                return ResultUtil.error("数据异常");
            request.setSystemLevelId(systemLevelId);
        }else{
            if(request.getSystemLevelId()==null){
                request.setSystemLevelId(this.getDefaultSystemLevelId(req));
            }
        }
        List<CategoryBean> responseList = categoryService.treeBeans(this.getSysBranchOfficeId(req),request.getSystemLevelId(),true,true,true);
        if(responseList==null)return ResultUtil.success();
        Page page = new Page<>(1,responseList.size(),responseList.size());
        page.setRecords(responseList);
        return ResultUtil.success(page);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增系统")
    public ResultUtil save(@RequestBody Category entity,HttpServletRequest req){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getParentId()==null)entity.setParentId(0l);
        if(entity.getCategoryId()!=null){
            categoryService.updateById(entity);
        }else{
            entity.setDeleted(0);
            entity.setSysBranchOfficeId(this.getSysBranchOfficeId(req));
            categoryService.save(entity);
        }
        Category update = new Category();
        update.setCategoryId(entity.getCategoryId());
        update.setCategoryPath("");
        setPathDg(entity, update);// 爷爷id.爸爸id.我的id.
        categoryService.updateById(update);
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询系统")
    public ResultUtil<CategoryBean> getById(@PathVariable String id){
        Category entity = categoryService.getById(id);
        if(entity!=null){
            CategoryBean bean = new CategoryBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改系统")
    public ResultUtil update(@RequestBody Category entity){
        if(entity.getParentId()==null)entity.setParentId(0l);
        entity.setCategoryPath("");
        setPathDg(entity, entity);// 爷爷id.爸爸id.我的id.
        categoryService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除系统")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        categoryService.removeById(id);
        return ResultUtil.success();
    }
    //计算  爷爷id.爸爸id.我的id.       顶级栏目：我的id.    最多支持5层
    public String setPath(Category entity1){
        long pid = entity1.getParentId().longValue();
        if(pid==0){
            return entity1.getCategoryId()+".";
        }
        Category entity2 = categoryService.getById(entity1.getParentId());
        if(entity2.getParentId().longValue()==0){
            return entity2.getCategoryId()+"."+entity1.getCategoryId()+".";
        }
        Category entity3 = categoryService.getById(entity2.getParentId());
        if(entity3.getParentId().longValue()==0){
            return entity3.getCategoryId()+"."+entity2.getCategoryId()+"."+entity1.getCategoryId()+".";
        }
        Category entity4 = categoryService.getById(entity3.getParentId());//倒数第四层
        if(entity4.getParentId().longValue()==0){
            return entity4.getCategoryId()+"."+entity3.getCategoryId()+"."+entity2.getCategoryId()+"."+entity1.getCategoryId()+".";
        }
        Category entity5 = categoryService.getById(entity4.getParentId());//倒数第五层
        return entity5.getCategoryId()+"."+entity4.getCategoryId()+"."+entity3.getCategoryId()+"."+entity2.getCategoryId()+"."+entity1.getCategoryId()+".";
    }
    //递归 支持无限层
    public void setPathDg(Category parent, Category needSetCategory){
        long pid = parent.getParentId().longValue();
        if(pid==0){
            needSetCategory.setCategoryPath(parent.getCategoryId()+"."+needSetCategory.getCategoryPath());
        }else{//当还存在父节点的时候，递归调用
            needSetCategory.setCategoryPath(parent.getCategoryId()+"."+needSetCategory.getCategoryPath());
            parent = categoryService.getById(parent.getParentId());
            setPathDg(parent , needSetCategory);//继续向下
        }
    }
}